package com.ror.game;

import com.ror.domain.User;
import com.ror.util.JDialogUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginJFrame extends JFrame {
    private static final JButton loginJButton = new LoginJButton();
    private static final JTextField unameJTextField = new JTextField();
    private static final JPasswordField pwdJPwdField = new JPasswordField();
    private static final JTextField verifyCodeJTextField = new JTextField();

    //当前登录窗口
    private static JFrame loginJFrame;
    //当前验证码
    private static String curVerifyCode = "abc12";

    private static class LoginJButton extends JButton implements ActionListener {
        public LoginJButton() {
            this.setText("登录");
            this.setBounds(0, 0, 100, 50);
            //自己实现ActionListener，传入自己
            this.addActionListener(this);
        }

        private void login() {
            System.out.println("尝试使你登录……（微软式中文）");

            String uname = unameJTextField.getText();
            String pwd = new String(pwdJPwdField.getPassword());
            String verifyCode = verifyCodeJTextField.getText();

            if (uname.isEmpty() || pwd.isEmpty() || verifyCode.isEmpty()) {
                JDialogUtil.show("登录失败：用户名、密码或验证码为空。");
                return;
            }

            if (!curVerifyCode.equals(verifyCode)) {
                JDialogUtil.show("登录失败：验证码错误。");
                return;
            }

            if (User.checkPwd(uname, pwd)) {
                System.out.println("登录成功！");
                loginJFrame.setVisible(false);
                new GameJFrame();
            } else {
                JDialogUtil.show("登录失败：用户名或密码错误。");
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            login();
        }
    }

    public static void reLogin() {
        if (loginJFrame == null) {
            new LoginJFrame();
        } else {
            loginJFrame.setVisible(true);
        }
    }

    public LoginJFrame() {
        loginJFrame = this;
        initJFrame();
        initView();
        this.setVisible(true);

        //debug
        verifyCodeJTextField.setText(curVerifyCode);
        unameJTextField.setText("admin");
        pwdJPwdField.setText("admin");
        JDialogUtil.show("默认账号：admin  密码：admin");
    }

    private void initJFrame() {
        this.setSize(300, 300);
        this.setTitle("拼图游戏-登录");
        //设置窗口置顶（流氓行为）
//        this.setAlwaysOnTop(true);
        //设置窗口默认居中
        this.setLocationRelativeTo(null);
        //设置关闭模式：EXIT_ON_CLOSE 关闭的同时关闭JVM进程
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消预设布局方式
        this.setLayout(null);
    }

    private void initView() {
        //添加用户名
        JLabel unameJLabel = new JLabel("用户名");
        unameJLabel.setBounds(20, 30, 50, 30);
        unameJLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        this.getContentPane().add(unameJLabel);

        unameJTextField.setBounds(80, 30, 200, 30);
        this.getContentPane().add(unameJTextField);

        //添加密码
        JLabel pwdJLabel = new JLabel("密码");
        pwdJLabel.setBounds(20, 80, 50, 30);
        pwdJLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        this.getContentPane().add(pwdJLabel);

        pwdJPwdField.setBounds(80, 80, 200, 30);
        this.getContentPane().add(pwdJPwdField);

        //添加验证码
        JLabel verifyJLabel = new JLabel("验证码");
        verifyJLabel.setBounds(20, 130, 50, 30);
        verifyJLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        this.getContentPane().add(verifyJLabel);

        verifyCodeJTextField.setBounds(80, 130, 100, 30);
        this.getContentPane().add(verifyCodeJTextField);

        JLabel verifyCodeJLabel = new JLabel(curVerifyCode);
        verifyCodeJLabel.setBounds(200, 130, 50, 30);
        this.getContentPane().add(verifyCodeJLabel);

        //添加登录按钮
        loginJButton.setBounds(50, 200, 100, 50);
        this.getContentPane().add(loginJButton);

        //添加注册按钮
        JButton registerJBtn = new JButton("注册");
        registerJBtn.addActionListener(e -> {
            JDialogUtil.show("注册功能尚未完工。");
//            new RegisterJFrame();
        });
        registerJBtn.setBounds(160, 200, 100, 50);
        this.getContentPane().add(registerJBtn);
    }
}
